package cn.net.xyan.easy.webapp.base.config;

import cn.net.xyan.easy.spring.core.config.AutoConfigManagerFactory;
import cn.net.xyan.easy.spring.core.config.value.ConfigKey;
import cn.net.xyan.easy.spring.core.utils.JsonHelper;
import cn.net.xyan.easy.webapp.base.WebAppBase;
import cn.net.xyan.easy.webapp.base.service.ISecurityService;
import cn.net.xyan.easy.webapp.base.service.ISystemConfigService;
import cn.net.xyan.easy.webapp.base.service.IWebModuleService;
import cn.net.xyan.easy.webapp.base.service.impl.DefaultWebModuleService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

/**
 * Created by xiashenpin on 16/1/17.
 */
@ComponentScan(basePackages = {"cn.net.xyan.easy.webapp.base"},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.annotation.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.controller.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.config.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.dao.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.api.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.entity.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "cn.net.xyan.easy.webapp.base.frontend.*")
        }
)
@Configuration("cn.net.xyan.easy.webapp.base.config.RootConfig")
public class RootConfig {

    public <T> T createInstance(ConfigKey key) throws IllegalAccessException, InstantiationException {
        Class<? extends T> implClass = AutoConfigManagerFactory.autoConfigManager().readValue(key, JsonHelper.javaType(Class.class));
        return implClass.newInstance();
    }

    @Bean
    public ISystemConfigService systemConfigService() throws IllegalAccessException, InstantiationException {

        return createInstance(WebAppBase.SystemConfigServiceImplClass);

//                Class<? extends ISystemConfigService> implClass =AutoConfigManagerFactory.autoConfigManager().readValue(
//                        WebAppBase.SystemConfigServiceImplClass, JsonHelper.javaType(Class.class));
//                return implClass.newInstance();
    }

    @Bean
    public ISecurityService securityService() throws IllegalAccessException, InstantiationException {
        return createInstance(WebAppBase.SecurityServiceImplClass);
//                Class<? extends ISecurityService> implClass =AutoConfigManagerFactory.autoConfigManager().readValue(
//                        WebAppBase.SecurityServiceImplClass, JsonHelper.javaType(Class.class));
//                return implClass.newInstance();
    }

    @Bean(name = "webModule")
    public IWebModuleService webModuleService() throws IllegalAccessException, InstantiationException {
        return createInstance(WebAppBase.WebModuleServiceImplClass);
//                Class<? extends IWebModuleService> implClass =AutoConfigManagerFactory.autoConfigManager().readValue(
//                        WebAppBase.WebModuleServiceImplClass, JsonHelper.javaType(Class.class));
//                return implClass.newInstance();
    }
}
